<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/6
 * Time: 15:23
 */

namespace app\mobilewx\controller;;


use app\Model\UsersModel;
use think\Controller;

class Login extends Common
{
    /**
     *  绑定手机页面
     */
    public function login()
    {
//        if (session('user_wx')){
//            $this->redirect('user/index');
//        }
        if (session('user_wx.mobile')!=NULL){
            $this->redirect('User/index');
        }
        return view('login');
    }
    /**
     * 登录验证验证
     */
    public function do_login()
    {
        $username = input('post.username');
        $password = input('post.password');
        $username = trim($username);
        $password = trim($password);
        $logic = new UsersLogic();
        $res = $logic->login($username, $password);

        if ($res['status']==1){
            $this->success($res['msg'],null,$res);
//            $this->redirect('User/index');
        }
        $this->error($res['msg']);
    }

    public function logout()
    {
        session('user_wx',null);

        setcookie('cn', '', time() - 3600, '/');
        $this->redirect('Login/login');

    }
    /*
     * 微信绑定信息验证
     */
    public function regver()
    {
        $getData= input('post.');
        $where=[
            'mobile'=>$getData['mobile'],
            'is_del'=>0,
            'openid'=>['<>','']//有微信id
        ];
        $getUser=UsersModel::where($where)->find();//手机是否已绑定其他微信
        if ($getUser){return retJson('','201','该手机已绑定其他微信');}
        //验证码
        $code=session($getData['mobile'])['code'];
        if ((string)$code!=$getData['mobile_code']){
            return retJson('','201','验证码错误');
        }
        $where_del=['is_del'=>0];
        $getUser=UsersModel::where('mobile',$getData['mobile'])->where($where_del)->find();//手机是否存在
        if (!$getUser){//手机号码还未录入（第一次在微信进入）---补全手机号码和密码
            $updata=[
                'mobile'=>$getData['mobile'],
                'password'=>encryption($getData['password']),
                'reg_time'=>time(),
            ];
            $user_id=session('user_wx.user_id');
            if (session('leader')){//有推荐人
                //查找推荐人id
                $get_user1= UsersModel::where('mobile',session('leader'))->where($where_del)->find();
                if ($get_user1){
                    $up_user1=$get_user1->user_id;//第一上级id
                    $insert['first_leader']=$up_user1;
                    $up_user2=$get_user1->first_leader;//第二上级id
                    $insert['second_leader']=$up_user2;
                }
                $setUser2=UsersModel::update($insert,['user_id'=>$user_id]);

                setDatabaseDef($setUser2,'推荐人添加失败');
            }
        }else{//手机号存在，----将微信信息转移到该手机下面
            //验证密码是否正确
            if (encryption($getData['password'])!=$getUser->password){
                return retJson('','201','密码错误');

            }
            $updata=[
                'openid'=>session('user_wx.openid'),
                'head_pic'=>session('user_wx.head_pic'),//头像
                'province'=>session('user_wx.province'),
                'city'=>session('user_wx.city'),
                'district'=>session('user_wx.district'),
            ];
            $user_id=$getUser->user_id;
            UsersModel::destroy(session('user_wx.user_id')); //删除原数据（）
        }
        $setUser=UsersModel::update($updata,['user_id'=>$user_id]);
        $public=new Publics();
        $public->upSess($user_id);//（session）更新
        return setDatabase($setUser,'手机号绑定成功');
    }
    /**
     * 忘记密码 页面跳转
     */
    public function forget_pwd(){
        return view('find_pwd');
    }
    /**
     * 修改密码(忘记密码)
     */
    public function changepwd(){

        if (input('mobile') == ''){
            return retJson('', 201, '请输入手机号！');
        }
        if (strlen(input('mobile')) != 11 ){
            return retJson('', 201, '请输入正确的手机号！');
        }
        if (input('mobile_code') == ''){
            return retJson('', 201, '请输入验证码！');
        }
        if (input('password') == ''){
            return retJson('', 201, '请输入新密码！');
        }
        if (input('newpass') == ''){
            return retJson('', 201, '请确认密码！');
        }
        if (input('password') != input('newpass')){
            return retJson('', 201, '两次密码不相同！');
        }
        if (session(input('mobile')) == ''){
            return retJson('', 201, '该号码未获取验证码,请检查号码是否正确或重新获取验证码！');
        };

        if (input('mobile_code') != session(input('mobile'))['code']){
            return retJson('', 201, '验证码错误！');
        }

        $res = UsersModel::update([
            'password'=>encryption(input('password'))],
            ['mobile'=>input('mobile'),'is_del'=>0]);

        if(!$res){
            return retJson('', 201, '修改失败！');
        }
        session(input('mobile'),null);

        return retJson('', 200, '修改成功！');
    }
    /**
     *短信验证 修改密码
     */
    public function smsver(){
        if (cookie(input('mobile')) == 1){
            return retJson('', 201, '距离上次验证码发送不超过一分钟！');
        }
        $code = rand(1000,9999);
        $mobile = input('mobile');

        $res = UsersModel::field('*')->where('mobile',$mobile)->where('is_del',0)->find();
        if (!$res){
            return retJson('', 201, '手机号未注册！');
        }
        session($mobile,array('mobile'=>$mobile,'code'=>$code));
        $send = sendMsg($mobile,$code);

        if ($send['Message'] != 'OK'){
            return retJson('', 201, '短信发送失败！');
        }
        cookie($mobile,1,60);
        return retJson('', 200, '短信已发送！');
    }

    /*
  *短信 注册用户
  */
    public function vercod()
    {
//        cookie('time',null);die();

        if (cookie('time') == 1){
            return retJson('', 201, '距离上次验证码发送不超过一分钟！');
        }
        $code = rand(1000,9999);
        $mobile = input('mobile');

        $res = UsersModel::field('*')->where('mobile',$mobile)->where('is_del',0)->find();
        if ($res&&$res->openid!=''){
            return retJson('', 201, '手机号已注册！');
        }
        session($mobile,array('mobile'=>$mobile,'code'=>$code));

        $send = sendMsg($mobile,$code);
//        return retJson('', 201, $send);

        if ($send['Message'] != 'OK'){
            return retJson('', 201, '短信发送失败！');
        }
        cookie('time',1,60);
//        return retJson('', 200, session($mobile));
        return retJson('', 200, '短信已发送！');
    }
}